import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

//一个通用的web加载页面
class CommonWeb extends StatefulWidget {
  String url;
  String title;

  CommonWeb(this.url, this.title);

  @override
  State<StatefulWidget> createState() => CommonWebState(this.url, this.title);
}

class CommonWebState extends State<CommonWeb> {
  String url;
  String title;

//  bool isLoading = true;
  double _progress = 0.0;

  FlutterWebviewPlugin _webViewPlugin = FlutterWebviewPlugin();

  CommonWebState(this.url, this.title);


  @override
  void initState() {
    super.initState();
    _webViewPlugin.onProgressChanged.listen((progress){
      setState(() {
        _progress = progress;
      });
    });

    _webViewPlugin.onStateChanged.listen((webViewState) {
      switch(webViewState.type){
        case WebViewState.finishLoad:
          getWebTitle();
          break;
        case WebViewState.shouldStart:
          break;
        case WebViewState.startLoad:
          break;
        case WebViewState.abortLoad:
          break;
      }
    });
  }


  @override
  void dispose() {
    _webViewPlugin.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    //依赖flutter_webview_plugin包的代码
    return WebviewScaffold(
      appBar: AppBar(
        title: Text(title),
        leading: IconButton(
          icon: Theme.of(context).platform == TargetPlatform.android
              ? Icon(Icons.arrow_back)
              : Icon(Icons.arrow_back_ios),
          onPressed: (){
            Navigator.pop(context);
          },
        ),
        bottom: PreferredSize(
          child: LinearProgressIndicator(
            backgroundColor: Colors.white70.withOpacity(0),
            value: _progress == 1.0 ? 0: _progress,
            valueColor: new AlwaysStoppedAnimation(Colors.redAccent),
          ),
          preferredSize: Size.fromHeight(3.0)
        ),
      ),

      url: url,
      withJavascript: true,

    );

    //一下代码是依赖flutter_webview
//    WebViewController _controller;
//    return Scaffold(
//      appBar: AppBar(
//          title: Text(title),
//          leading: IconButton(
//              icon: Icon(Icons.arrow_back_ios),
//              onPressed: () {
//                Navigator.pop(context);
//              })),
//      body: Stack(
//        children: <Widget>[
//          WebView(
//            //加载的URL地址
//            initialUrl: url,
//            //允许JS执行
//            javascriptMode: JavascriptMode.unrestricted,
//            //设置controller
//            onWebViewCreated: (controller){
//              _controller = controller;
//            },
//            //获取网页上的title
//            onPageFinished: (url){
//              _controller.evaluateJavascript("document.title").then((result){
//                setState(() {
//                  title = result;
//                  //网页加载完成
//                  isLoading = false;
//                });
//              });
//            },
//            //开始加载网页
//            navigationDelegate: (NavigationRequest request){
//              var url = request.url;
//              setState(() {
//                //开始访问时，加载网页
//                isLoading = true;
//              });
//              return NavigationDecision.navigate;
//            },
//          ),
//          isLoading? Container(
//            child: Center(
//              child: CircularProgressIndicator(),
//            ),
//          ):Container(),
//        ],
//      ),
//    );
  }

  //获取页面中的title
  Future<void> getWebTitle() async{
    String script = 'window.document.title';
    var title = await
    _webViewPlugin.evalJavascript(script);
    setState(() {
      this.title = title;
    });
  }
}
